#!/usr/bin/env expect
############################################################################
# Purpose: Test of TotalView termination logic for srun.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
# Note:    This script generates and then deletes files in the working directory
#          named test7.5.prog
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Copyright (C) 2008-2009 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "7.5"
set exit_code   0
set file_prog   "test$test_id.prog"
set matches     0
set fini_cnt    0
set usr1cnt     0
set usr2cnt     0

print_header $test_id

if {[test_alps]} {
	send_user "\nWARNING: This test is incompatible with Cray systems\n"
	exit $exit_code
} elseif {[test_launch_poe]} {
	send_user "\nWARNING: This test is incompatible with POE systems\n"
	exit $exit_code
}

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $file_prog
exec $bin_cc -O ${file_prog}.c -o $file_prog
exec $bin_chmod 700 $file_prog

#
# Get uid
#
set uid [get_my_uid]


#
# Spawn initial program via srun and terminate with SIGTERM
# Note: For systems supporting proper pthreads, instead use
#       exec $bin_kill -TERM $srun_pid, otherwise we need pkill
#       and can get multiple signals delivered
# Note: We send the signal right after task startup rather than
#	interspersed with messages because some versions of
#	Expect have difficulties handling unbuffered srun output
#
set timeout $max_job_delay
set srun_pid [spawn $srun -N1 -t1 --debugger-test --unbuffered ./$file_prog]
expect {
	-re "task:.*, host:.*, pid:.*, executable:.*" {
		incr matches
		# sleep to make sure the process is actually running
		exec $bin_sleep 1
		exec $bin_kill -TERM $srun_pid
		send_user "\nSent SIGTERM\n"
		exp_continue
	}
	-re "Received signal" {
		send_user "\nFAILURE: unexpected signal processed\n"
		set exit_code 1
		send_user "\nSent SIGTERM\n"
		exp_continue
	}
	-re "WAITING" {
		send_user "\nFAILURE: job not stopped in debugger mode\n"
		set exit_code 1
		send_user "\nSent SIGTERM\n"
		exp_continue
	}
	-re "TIME LIMIT" {
		send_user "\nFAILURE: job not terminated with SIGINT\n"
		set exit_code 1
		exp_continue
	}
	-re "error.*not running" {
		send_user "\nDon't worry about the error...\n"
		exp_continue
	}
	-re "FINI" {
		incr fini_cnt
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		send_user "\nEOF\n"
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: srun failed to initialize properly ($matches != 1)\n"
	set exit_code 1
}
if {$fini_cnt > 0} {
	send_user "\nFAILURE: srun failed to terminate properly ($fini_cnt > 0)\n"
	set exit_code 1
}

if {$exit_code == 0} {
	send_user "\n\nSo far, so good\n\n"
} else {
	exit $exit_code
}

#
# Spawn initial program via srun and terminate with SIGINT * 3
#
set matches  0
set fini_cnt 0
set srun_pid [spawn $srun -N1 -t1 --debugger-test --unbuffered ./$file_prog]
expect {
	-re "task:.*, host:.*, pid:.*, executable:.*" {
		incr matches
		# sleep to make sure the process is actually running
		exec $bin_sleep 1
		exec $bin_kill -INT $srun_pid
		exec $bin_kill -INT $srun_pid
		send_user "\nSent SIGINT * 2\n"
		exp_continue
	}
	-re "Received signal" {
		send_user "\nFAILURE: unexpected signal processed\n"
		set exit_code 1
		send_user "\nSent SIGTERM\n"
		exp_continue
	}
	-re "WAITING" {
		send_user "\nFAILURE: job not stopped in debugger mode\n"
		set exit_code 1
		send_user "\nSent SIGTERM\n"
		exp_continue
	}
	-re "TIME LIMIT" {
		send_user "\nFAILURE: job not terminated with SIGINT\n"
		set exit_code 1
		exp_continue
	}
	-re "error.*not running" {
		send_user "\nDon't worry about the error...\n"
		exp_continue
	}
	-re "FINI" {
		incr fini_cnt
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		send_user "\nEOF\n"
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: srun failed to initialize properly ($matches != 1)\n"
	set exit_code 1
}
if {$fini_cnt > 0} {
	send_user "\nFAILURE: srun failed to terminate properly ($fini_cnt > 0)\n"
	set exit_code 1
}

#
# Post-processing
#
if {$exit_code == 0} {
	exec $bin_rm -f $file_prog
	send_user "\nSUCCESS\n"
}
exit $exit_code
